﻿#include "setup.h"
#include "ui_setup.h"

setup::setup(QWidget *parent, Config *mainConf)
    : QWidget(parent)
    , ui(new Ui::setup)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint |Qt::FramelessWindowHint);

    isSave = false;
    conf = mainConf;

    resize(conf->confVar.WindowSetupWidth,conf->confVar.WindowSetupHeight);

    initUI();

    ui->spinBoxSlowRate->setValue(conf->uimVar.CanLeftRate/SLOWRATE);
    ui->spinBoxFastRate->setValue(conf->uimVar.CanLeftFastRate/FASTRATE);
    ui->spinBoxLeftStep->setValue(conf->uimVar.CanLeftStep/LEFTSTEP);
    ui->spinBoxRightStep->setValue(conf->uimVar.CanRightStep/RIGHTSTEP);
}

setup::~setup()
{
    delete ui;
}

void setup::initUI(){
    ui->labelTitle->setText("设置参数");
    ui->labelTitle->setStyleSheet(QString("%0 %1")
                                      .arg(getLabelSize(60,
                                                        this->size().width(),
                                                        80))
                                      .arg(conf->shellVar.LabelShell1)
                                  );
    //LEFT
    ui->labelLeft->setText(conf->setupVar.LabelLeftTitleText);
    ui->labelLeft->setStyleSheet(QString("%0 %1")
                                     .arg(getLabelSize(conf->setupVar.LabelLeftTitleSize,
                                                       conf->setupVar.LabelLeftTitleWidth,
                                                       conf->setupVar.LabelLeftTitleHeight))
                                     .arg(conf->shellVar.LabelShell2)
                                 );
    ui->spinBoxLeftStep->setStyleSheet(QString("%0 %1")
                                           .arg(getSpinBoxSize(conf->setupVar.SpinBoxLeftValueSize,
                                                               conf->setupVar.SpinBoxLeftValueWidth,
                                                               conf->setupVar.SpinBoxLeftValueHeight))
                                           .arg(conf->shellVar.SpinBoxShell)
                                       );
    ui->pushButtonAddLeftStep->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                 .arg(getPushButtonSize(conf->setupVar.PushButtonLeftAddSize,
                                                                        conf->setupVar.PushButtonLeftAddWidth,
                                                                        conf->setupVar.PushButtonLeftAddHeight))
                                                 .arg(conf->shellVar.PushButtonShell1)
                                                 .arg(conf->shellVar.PushButtonShellHover1)
                                                 .arg(conf->shellVar.PushButtonShellPressed1)
                                                 .arg(conf->shellVar.PushButtonShellDisabled)
                                             );
    ui->pushButtonDecLeftStep->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                 .arg(getPushButtonSize(conf->setupVar.PushButtonLeftDecSize,
                                                                        conf->setupVar.PushButtonLeftDecWidth,
                                                                        conf->setupVar.PushButtonLeftDecHeight))
                                                 .arg(conf->shellVar.PushButtonShell1)
                                                 .arg(conf->shellVar.PushButtonShellHover1)
                                                 .arg(conf->shellVar.PushButtonShellPressed1)
                                                 .arg(conf->shellVar.PushButtonShellDisabled)
                                             );
    //RIGHT
    ui->labelRight->setText(conf->setupVar.LabelRightTitleText);
    ui->labelRight->setStyleSheet(QString("%0 %1")
                                      .arg(getLabelSize(conf->setupVar.LabelRightTitleSize,
                                                        conf->setupVar.LabelRightTitleWidth,
                                                        conf->setupVar.LabelRightTitleHeight))
                                      .arg(conf->shellVar.LabelShell2)
                                  );
    ui->spinBoxRightStep->setStyleSheet(QString("%0 %1")
                                           .arg(getSpinBoxSize(60,
                                                               180,
                                                               80))
                                           .arg(conf->shellVar.SpinBoxShell)
                                       );
    ui->pushButtonAddRightStep->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                  .arg(getPushButtonSize(70,
                                                                         160,
                                                                         80))
                                                  .arg(conf->shellVar.PushButtonShell1)
                                                  .arg(conf->shellVar.PushButtonShellHover1)
                                                  .arg(conf->shellVar.PushButtonShellPressed1)
                                                  .arg(conf->shellVar.PushButtonShellDisabled)
                                              );
    ui->pushButtonDecRightStep->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                  .arg(getPushButtonSize(70,
                                                                         160,
                                                                         80))
                                                  .arg(conf->shellVar.PushButtonShell1)
                                                  .arg(conf->shellVar.PushButtonShellHover1)
                                                  .arg(conf->shellVar.PushButtonShellPressed1)
                                                  .arg(conf->shellVar.PushButtonShellDisabled)
                                              );
    //SLOW
    ui->labelSlow->setText(conf->setupVar.LabelSlowTitleText);
    ui->labelSlow->setStyleSheet(QString("%0 %1")
                                     .arg(getLabelSize(60,
                                                       200,
                                                       80))
                                     .arg(conf->shellVar.LabelShell2)
                                 );
    ui->spinBoxSlowRate->setStyleSheet(QString("%0 %1")
                                           .arg(getSpinBoxSize(60,
                                                               180,
                                                               80))
                                           .arg(conf->shellVar.SpinBoxShell)
                                       );
    ui->pushButtonAddSlowRate->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                 .arg(getPushButtonSize(70,
                                                                        160,
                                                                        80))
                                                 .arg(conf->shellVar.PushButtonShell1)
                                                 .arg(conf->shellVar.PushButtonShellHover1)
                                                 .arg(conf->shellVar.PushButtonShellPressed1)
                                                 .arg(conf->shellVar.PushButtonShellDisabled)
                                             );
    ui->pushButtonDecSlowRate->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                 .arg(getPushButtonSize(70,
                                                                        160,
                                                                        80))
                                                 .arg(conf->shellVar.PushButtonShell1)
                                                 .arg(conf->shellVar.PushButtonShellHover1)
                                                 .arg(conf->shellVar.PushButtonShellPressed1)
                                                 .arg(conf->shellVar.PushButtonShellDisabled)
                                             );
    //FAST
    ui->labelFast->setText(conf->setupVar.LabelFastTitleText);
    ui->labelFast->setStyleSheet(QString("%0 %1")
                                     .arg(getLabelSize(60,
                                                       200,
                                                       80))
                                     .arg(conf->shellVar.LabelShell2)
                                 );
    ui->spinBoxFastRate->setStyleSheet(QString("%0 %1")
                                           .arg(getSpinBoxSize(60,
                                                               180,
                                                               80))
                                           .arg(conf->shellVar.SpinBoxShell)
                                       );
    ui->pushButtonAddFastRate->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                 .arg(getPushButtonSize(70,
                                                                        160,
                                                                        80))
                                                 .arg(conf->shellVar.PushButtonShell1)
                                                 .arg(conf->shellVar.PushButtonShellHover1)
                                                 .arg(conf->shellVar.PushButtonShellPressed1)
                                                 .arg(conf->shellVar.PushButtonShellDisabled)
                                             );
    ui->pushButtonDecFastRate->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                 .arg(getPushButtonSize(70,
                                                                        160,
                                                                        80))
                                                 .arg(conf->shellVar.PushButtonShell1)
                                                 .arg(conf->shellVar.PushButtonShellHover1)
                                                 .arg(conf->shellVar.PushButtonShellPressed1)
                                                 .arg(conf->shellVar.PushButtonShellDisabled)
                                             );
    //SAVE/CANCEL
    ui->pushButtonSave->setText(conf->setupVar.PushButtonSaveText);
    ui->pushButtonSave->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                           .arg(getPushButtonSize(conf->setupVar.PushButtonSaveSize,
                                                                  conf->setupVar.PushButtonSaveWidth,
                                                                  conf->setupVar.PushButtonSaveHeight))
                                          .arg(conf->shellVar.PushButtonShell1)
                                          .arg(conf->shellVar.PushButtonShellHover1)
                                          .arg(conf->shellVar.PushButtonShellPressed1)
                                           .arg(conf->shellVar.PushButtonShellDisabled)
                                       );
    ui->pushButtonCancel->setText(conf->setupVar.PushButtonCancelText);
    ui->pushButtonCancel->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                            .arg(getPushButtonSize(conf->setupVar.PushButtonCancelSize,
                                                                   conf->setupVar.PushButtonCancelWidth,
                                                                   conf->setupVar.PushButtonCancelHeight))
                                            .arg(conf->shellVar.PushButtonShell1)
                                            .arg(conf->shellVar.PushButtonShellHover1)
                                            .arg(conf->shellVar.PushButtonShellPressed1)
                                            .arg(conf->shellVar.PushButtonShellDisabled)
                                        );
}

void setup::on_pushButtonSave_clicked()
{
    if(isSave){
        conf->uimVar.CanLeftStep = ui->spinBoxLeftStep->value()*LEFTSTEP;
        conf->uimVar.CanRightStep = ui->spinBoxRightStep->value()*RIGHTSTEP;
        conf->uimVar.CanLeftFastRate = ui->spinBoxFastRate->value()*FASTRATE;
        conf->uimVar.CanRightFastRate = ui->spinBoxFastRate->value()*FASTRATE;
        conf->uimVar.CanLeftRate = ui->spinBoxSlowRate->value()*SLOWRATE;
        conf->uimVar.CanRightRate = ui->spinBoxSlowRate->value()*SLOWRATE;
        conf->saveUim();
        emit setupSave();
    }
    close();
}


void setup::on_pushButtonCancel_clicked()
{
    close();
}


void setup::on_spinBoxLeftStep_valueChanged(int arg1)
{
    isSave = true;
}


void setup::on_pushButtonAddLeftStep_clicked()
{
    if(ui->spinBoxLeftStep->value()<ui->spinBoxLeftStep->maximum()){
        ui->spinBoxLeftStep->setValue(ui->spinBoxLeftStep->value()+1);
    }

}


void setup::on_pushButtonDecLeftStep_clicked()
{
    if(ui->spinBoxLeftStep->value()>0){
        ui->spinBoxLeftStep->setValue(ui->spinBoxLeftStep->value()-1);
    }
}


void setup::on_spinBoxRightStep_valueChanged(int arg1)
{
    isSave = true;
}


void setup::on_pushButtonAddRightStep_clicked()
{
    if(ui->spinBoxRightStep->value()<ui->spinBoxRightStep->maximum()){
        ui->spinBoxRightStep->setValue(ui->spinBoxRightStep->value()+1);
    }

}


void setup::on_pushButtonDecRightStep_clicked()
{
    if(ui->spinBoxRightStep->value()>0){
        ui->spinBoxRightStep->setValue(ui->spinBoxRightStep->value()-1);
    }
}


void setup::on_spinBoxSlowRate_valueChanged(int arg1)
{
    isSave = true;
}


void setup::on_pushButtonAddSlowRate_clicked()
{
    if(ui->spinBoxSlowRate->value()<ui->spinBoxSlowRate->maximum()){
        ui->spinBoxSlowRate->setValue(ui->spinBoxSlowRate->value()+1);
    }

}


void setup::on_pushButtonDecSlowRate_clicked()
{
    if(ui->spinBoxSlowRate->value()>0){
        ui->spinBoxSlowRate->setValue(ui->spinBoxSlowRate->value()-1);
    }
}


void setup::on_spinBoxFastRate_valueChanged(int arg1)
{
    isSave = true;
}


void setup::on_pushButtonAddFastRate_clicked()
{
    if(ui->spinBoxFastRate->value()<ui->spinBoxFastRate->maximum()){
        ui->spinBoxFastRate->setValue(ui->spinBoxFastRate->value()+1);
    }
}


void setup::on_pushButtonDecFastRate_clicked()
{
    if(ui->spinBoxFastRate->value()>0){
        ui->spinBoxFastRate->setValue(ui->spinBoxFastRate->value()-1);
    }
}
